/*
This file creates the demographic comparison of WI to other states with abortion legislation pending.

NOTE: Can only run if you have access to the NVSS natality files, otherwise will not run

*/

  infix  year 1-4 str state_abbrev 5-6 state_fips 7-8 county_fips 9-11 registry 12-13 ///
  race 14 origin 15 sex 16 age 17-18 pop 19-27 using "${rawdata}\countypop.txt", clear
  g race2 = .
  foreach num of numlist 1/4{
  replace race2 = `num' if race == `num' & origin == 0
  
  }
  replace race2 = 5 if origin == 1
  replace race = race2
    collapse (sum) pop, by (state_fips year age race sex) 
reshape wide pop, i(state_fips race sex year) j(age)

egen pop_allage = rowtotal(pop*)
rename pop5 pop_F15to19
rename pop6 pop_F20to24
rename pop7 pop_F25to29
rename pop8 pop_F30to34
rename pop9 pop_F35to39
rename pop10 pop_F40to44
 keep pop_* state_fips race sex year
 egen pop_15to44 = rowtotal(pop_F*)
 
 reshape wide pop_F15to19 pop_F20to24  pop_F25to29 pop_F30to34 pop_F35to39 pop_F40to44 pop_15to44 pop_allage, i(state_fips sex year) j(race)
 renvars *41 *91, postsub(1 white)
 renvars *42 *92, postsub(2 black)
 renvars *43 *93, postsub(3 natamer)
 renvars *44 *94, postsub(4 asian)
 renvars *45 *95, postsub(5 hisp) 
 rename pop_allage1 pop_allagewhite
 rename pop_allage2 pop_allageblack
 rename pop_allage3 pop_allagenatamer
 rename pop_allage4 pop_allageasian
  rename pop_allage5 pop_allagehisp
  
 egen pop_allage_allrace = rowtotal(pop_allage*)
 egen pop_15to44_allrace = rowtotal(pop_15to44*)
 
 g perc_black = pop_allageblack/pop_allage_allrace
 g perc_white = pop_allagewhite/pop_allage_allrace
 g perc_natamer = pop_allagenatamer/pop_allage_allrace
 g perc_asian = pop_allageasian/pop_allage_allrace
 g perc_hisp   = pop_allagehisp/pop_allage_allrace
 
 g perc_black_15to44 = pop_15to44black/pop_15to44_allrace
 g perc_white_15to44 = pop_15to44white/pop_15to44_allrace
 g perc_natamer_15to44 = pop_15to44natamer/pop_15to44_allrace
 g perc_asian_15to44 = pop_15to44asian/pop_15to44_allrace
  g perc_hisp_15to44 = pop_15to44hisp/pop_15to44_allrace
 preserve
 keep if state_fips == 55 | state_fips == 48 | state_fips == 29 | state_fips == 21| state_fips == 5| ///
 state_fips == 39  | state_fips == 18 | state_fips == 22 | state_fips == 28

 summ perc_* if sex == 2& year > 2009 & state_fips == 55
 summ perc_* if sex == 2& year > 2009 & state_fips == 48
  summ perc_* if sex == 2& year > 2009 & state_fips != 55 & state_fips != 48
   
 g WI_ornot = (state_fips == 55)
  g TX_ornot = (state_fips == 48)
 local racetype white black natamer asian hisp
 foreach race of local racetype{
 ttest perc_`race' if sex == 2& year > 2009 &state_fips != 48, by(WI_ornot)
 
    

 ttest perc_`race' if sex == 2& year > 2009 &state_fips != 55, by(TX_ornot)
 }
 restore
 
 
 keep if sex == 2 & year > 2009 
 
 preserve 
use  "${dtasave}\natality_1990to2017.dta" ,clear
*** STEP ONE: Creating Indicators for each category
keep if mother_age >= 15 & mother_age <= 44 // Keeping only mothers who are of general birth age
keep if dob_yy >= 2009  // keeping only post-2009 sample

gen allbirth = 1 // For all births
gen teenbirth  = (mother_age < 20)
gen twentybirth = (mother_age >= 20 & mother_age <30)
gen thirtyplusbirth = (mother_age >= 30 & mother_age != .)

gen whitebirth = (mother_race  == 1 & mother_hispanic == 0)
gen blackbirth = (mother_race == 2 & mother_hispanic == 0)
gen hispbirth = (mother_hispanic == 1)
gen asianbirth= (mother_race == 4 & mother_hispanic == 0)
gen otherbirth= (mother_race != 1 & mother_race != 2 & mother_race != 3 & mother_hispanic != 1)
replace married_mother = 1 - married_mother
gen marriedbirth = (married_mother == 1)
gen unmarriedbirth = (married_mother == 0)
gen county =county_fips_res if county_fips_res >= 1000
replace county=county_fips_res+statefip_mother*1000 if county_fips_res < 1000

** STEP TWO: Cleaning County- Variable and collapsing by state
g year = dob_yy
g state_fips= statefip_mother
collapse (sum) allbirth teenbirth twentybirth thirtyplusbirth whitebirth ///
 blackbirth hispbirth asianbirth otherbirth marriedbirth unmarriedbirth (mean) statefip_mother county dob_yy , by(state_fips year)
 tempfile birthsbystate
 save `birthsbystate'

 restore 
 merge 1:1 state_fips year using `birthsbystate'
 
 cap drop birthrate*
 
 g birthrate = 1000*allbirth/pop_15to44_allrace
  local racetype white black asian hisp
 foreach race of local racetype{
 g birthrate_`race'= 1000*`race'birth/pop_15to44`race'
}
g birthrate_nonwhite = 1000* (blackbirth+asianbirth+hispbirth)/ (pop_15to44black+pop_15to44asian +pop_15to44hisp)
egen pop_teen = rowtotal( pop_F15*)
g pop_nonteen = pop_15to44_allrace - pop_teen
g birthrate_teen = 1000*teenbirth/pop_teen
g birthrate_nonteen =1000*(allbirth-teenbirth)/pop_nonteen


g state_with_case = (state_fips == 29 | state_fips == 21 | state_fips == 5 | state_fips == 39 | state_fips == 22 | state_fips == 18 | state_fips == 28)

cap drop _merge
merge 1:1 state_fips year using "${dtasave}\abortions_allstates.dta"
cap drop _merge
 g abortionrate = 1000*ResCount/pop_15to44_allrace
 
 
eststo clear

estpost summ birthrate abortionrate perc_*_15to44 if year >= 2010 & year <= 2016  & state_fips == 55
eststo WI2010
estpost summ  birthrate abortionrate perc_*_15to44  if year >= 2010 & year <= 2016  & state_with_case == 1 
eststo abortioncase2010
estpost summ  birthrate abortionrate perc_*_15to44  if year >= 2010  & year <= 2016 
eststo USA2010
estpost summ  birthrate abortionrate perc_*_15to44  if year >= 2010  & year <= 2016 & state_fips == 48
eststo TX2010



label var birthrate "Birth Rate, per 1000 women"
label var birthrate_white "Non-Hispanic White Birth Rate, per 1000 women"
label var birthrate_nonwhite "Non-White Birth Rate, per 1000 women"
label var birthrate_black "Black Birth Rate, per 1000 women"
label var birthrate_hisp "Hispanic Birth Rate, per 1000 women"
label var birthrate_asian "Asian Birth Rate, per 1000 women"
label var birthrate_teen "Teen Birth Rate, per 1000 women"
label var birthrate_nonteen "Non-Teen Birth Rate, per 1000 women"
label var perc_white_15to44 "Women 15 to 44, Percent Non-Hispanic White"
label var perc_black_15to44 "Women 15 to 44, Percent Black"
label var perc_hisp_15to44 "Women 15 to 44, Percent Hispanic"
label var perc_asian_15to44 "Women 15 to 44, Percent Asian"
label var abortionrate "Abortion Rate, per 1000 women"

		esttab WI2010 abortioncase2010 USA2010  TX2010 ///
		 using "TableA1.tex", ///
		main(mean) aux(sd) nonumbers ///
		mtitles("Wisconsin" "States with Court Cases" "USA" "Texas"  )  ///
		label title("Demographic Comparison: Wisconsin vs. Other States") replace
	   



g WI = state_fips == 55
g TX = state_fips == 48

local rates birthrate birthrate_white birthrate_black birthrate_hisp birthrate_asian birthrate_teen birthrate_nonteen

summ birthrate if WI == 1 & year == 2010
ttest birthrate == 61.03698 if year == 2010 & state_with_case == 1
foreach g of local rates{
display("`g'")
ttest `g' if year >= 2010, by(WI)
preserve
 keep if WI == 1 | state_with_case == 1
 ttest `g' if year >= 2010 , by(WI)
restore
ttest `g' if year >= 2010 , by(TX)
preserve
 keep if TX == 1 | state_with_case == 1
 ttest `g' if year >= 2010 , by(TX)
restore
}

